<?php

namespace App\ZhCN;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use App\Tools\UrlHelper;
use Phoenix\Log\Log4p as logger;

/**
 * 首页
 */
class Member {

    private function __Controller() {}

    private function __Value($__PACKAGE__, $__ROOT__, $__RM__, $setting, $cfg, $__LANGUAGE_ID__) {}

    protected function __Inject($session, Service\Archives $servArc, Service\WeiXin $weixin, Repository\Archives $repoArc,
                                Repository\Category $category, Repository\Member $repoMem) {}

    public function login($__Route = array('/member/login', 'login'))
    {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            if ($this->__LANGUAGE_ID__ == 1) {
                $_model['pageSeoTitle'] = 'Bondent Group';
            }
       
            $this->session->destory('member');

            $_model['isMobile'] = UrlHelper::pdMobileUrl();
            
            //首页底部链接
            return array(
                'model' => $_model,
                'view' => 'member/login'
            );
        }
    }

    //微信手机授权登陆
    public function getCode($__Route = '/wechat/bind') {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            if ($this->__LANGUAGE_ID__ == 1) {
                $_model['pageSeoTitle'] = 'Bondent Group';
            }

            $openid = $this->session->openid;
            if (!$openid) {
                $openid = $this->weixin->getOpenid('/wechat/bind');
                $this->session->destory('openid');
                $this->session->openid = array(
                    'openid' => $openid['openid'],
                    'headimgurl' => $openid['headimgurl'],
                    'nickname' => $openid['nickname'],
                );
                $_model['openid'] = $this->session->openid;
                header('Location: /wechat/bind');exit;
            }
            $_model['openid'] = $this->session->openid;
            if ($_model['openid']['openid']) {
                $_rs = $this->repoMem->findMember($_model['openid']['openid'], 2);

                if (count($_rs) > 0) {
                    $this->session->member = array(
                        'member_id' => $_rs->member_id,
                    );
                    $url = $_GET['url'] ? $_GET['url'] : '/member/center';
                    header('Location:' . $url);exit;
                }
            }
            return array(
                'model' => $_model,
                'view' => 'member/bind'
            );
        }
    }

    public function register($__Route = array('/register'))
    {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            if ($this->__LANGUAGE_ID__ == 1) {
                $_model['pageSeoTitle'] = 'Bondent Group';
            }

            //首页底部链接
            return array(
                'model' => $_model,
                'view' => 'member/register'

              );
        }
    }

    public function password($__Route = array('/member/password'))
    {
        if ($this->servArc->chkCategoryDataView()) {
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            if ($this->__LANGUAGE_ID__ == 1) {
                $_model['pageSeoTitle'] = 'Bondent Group';
            }

            //首页底部链接
            return array(
                'model' => $_model,
                'view' => 'member/password'
            );
        }
    }
}
